mod controller;
mod dto;
mod entity;
mod middleware;

use crate::middleware::auth_token;
use crate::controller::router;
use hypers::prelude::{Result, Router};
use once_cell::sync::Lazy;
use rbatis::RBatis;
#[macro_use]
extern crate rbatis;

pub static RB: Lazy<RBatis> = Lazy::new(RBatis::new);

#[tokio::main]
async fn main() -> Result<()> {
    log4rs::init_file("log4rs.yaml", Default::default()).unwrap();
    let mysql_url = "mysql://root:root@127.0.0.1:3306/rustdb";
    RB.init(rbdc_mysql::driver::MysqlDriver {}, mysql_url)
        .expect("数据库连接错误");

    let mut root = Router::default();
    router(&mut root);
    root.hook(auth_token, vec!["/api/{*p}"],vec!["/api/login"]);
    root.swagger("swagger-ui", None); 
    println!("root = {:#?}",root);
    let addr = "0.0.0.0:8100";
    // log::info!("Vist browser http://{}/swagger-ui/", addr);
    log::info!("Vist browser http://{}/api", addr);
    let listener = hypers::TcpListener::bind(addr).await?;
    hypers::listen(root, listener).await
}
